<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\letter\LetterDiary;

/**
 * 日记相关
 */
class Diary extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 日记列表
     * @return void
     * @throws \think\exception\DbException
     */
    public function getDiaryLst()
    {
        $status = $this->request->param('status') ?? -1;
        $page = $this->request->param('page') ?: 1;
        $limit = $this->request->param('limit') ?: 10;
        $list = LetterDiary::where(['uid' => $this->auth->id])
            ->where(function ($query) use ($status) {
                if ($status > -1)
                    $query->where(['status' => $status]);
            })
            ->order('id', 'desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);

        $this->success('获取成功！', $list);
    }

    /**
     * 保存日记
     * @return void
     * @throws \think\exception\DbException
     */
    public function saveDiary()
    {
        $d_id = $this->request->param('d_id') ?: 0;
        $content = $this->request->param('content');
        if (!$content)
            $this->error('请填写日记内容吧~');

        $uid = $this->auth->id;
        if (!$d_id) {
            $re = LetterDiary::create([
                'uid' => $this->auth->id,
                'content' => $content,
            ]);
        } else {
            $info = LetterDiary::get(['id' => $d_id, 'uid' => $uid]);
            if (!$info)
                $this->error('数据不存在~');
            $info->content = $content;
            $info->updatetime = time();
            $re = $info->save();
        }
        if (!$re)
            $this->error('保存失败，请稍后再试~');

        $this->success('保存成功！');
    }

    /**
     * 日记详情
     * @return void
     * @throws \think\exception\DbException
     */
    public function getDiaryInfo()
    {
        $d_id = $this->request->param('d_id') ?: 0;
        $info = LetterDiary::get(['id' => $d_id, 'uid' => $this->auth->id]);
        if (!$info)
            $this->error('数据不存在~');

        $this->success('获取成功！', $info);
    }

    /**
     * 设置日记状态
     * @return void
     * @throws \think\exception\DbException
     */
    public function setDiaryStatus()
    {
        $d_id = $this->request->param('d_id');
        $info = LetterDiary::get(['id' => $d_id, 'uid' => $this->auth->id]);
        if (!$info)
            $this->error('数据不存在~');

        $info->status = $info['status'] == 1 ? 0 : 1;
        $info->save();
        $this->success('操作成功！');
    }

    /**
     * 删除日记
     * @return void
     * @throws \think\exception\DbException
     */
    public function delDiary()
    {
        $d_id = $this->request->param('d_id');
        $info = LetterDiary::get(['id' => $d_id, 'uid' => $this->auth->id]);
        if (!$info)
            $this->error('数据不存在~');

        $info->delete();
        $this->success('删除成功！');
    }
}
